//
//  SheetView.m
//  IBDP
//  联动表格
//  Created by LYPC on 2017/7/25.
//  Copyright © 2017年 cattsoft. All rights reserved.
//

#import "SheetView.h"
#import "SheetCell.h"

@interface SheetView () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic,strong) UITableView *lTableView;
@property (nonatomic,strong) UITableView *rTableView;
@property (nonatomic,strong) UIScrollView *buttomScrollView;

@property (nonatomic,strong) NSArray *lDatas;
@property (nonatomic,strong) NSArray *rDatas;

@property (nonatomic,strong) NSArray *lTitles;
@property (nonatomic,strong) NSArray *rTitles;

@end

@implementation SheetView

- (instancetype)init {
    if (self = [super init]) {
        self.backgroundColor = [UIColor blackColor];
    }
    return self;
}

- (void)loadDatasLeftTitles:(NSArray *)lTitles
                  leftDatas:(NSArray *)lDatas
                rightTitles:(NSArray *)rTitles
                 rightDatas:(NSArray *)rDatas {
    self.lTitles = lTitles;
    self.lDatas = lDatas;
    
    self.rTitles = rTitles;
    self.rDatas = rDatas;
    
    [self.lTableView reloadData];
    [self.rTableView reloadData];
}

#pragma mark UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.lTableView) {
        return self.lDatas.count;
    }else if (tableView == self.rTableView) {
        return self.rDatas.count;
    }else {
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.lTableView) {
        SheetCell *cell = [SheetCell cellWithTableView:tableView cellContents:self.lDatas[indexPath.row] type:@"L"];
        return cell;
    }else {
        SheetCell *cell = [SheetCell cellWithTableView:tableView cellContents:self.rDatas[indexPath.row] type:@"R"];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return HeaderViewH;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (tableView == self.lTableView) {
        UIView *lHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ItemW*self.lTitles.count, HeaderViewH)];
        lHeaderView.backgroundColor = [UIColor blackColor];
        [self setupTitleView:lHeaderView titles:self.lTitles];
        return lHeaderView;
    }else {
        UIView *rHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ItemW*self.rTitles.count, HeaderViewH)];
        rHeaderView.backgroundColor = [UIColor blackColor];
        [self setupTitleView:rHeaderView titles:self.rTitles];
        return rHeaderView;
    }
}

- (void)setupTitleView:(UIView *)view titles:(NSArray *)titles {
    
    [titles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        int rowX = (int)idx%titles.count;
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(ItemW*rowX, 0, ItemW, HeaderViewH)];
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:Font];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = titles[idx];
        label.numberOfLines = 0;
        label.layer.masksToBounds = YES;
        label.layer.borderColor = [UIColor blueColor].CGColor;
        label.layer.borderWidth = 0.5;
        [view addSubview:label];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.lTableView) {
        [self tableView:self.rTableView scrollFollowTheOther:self.lTableView];
    }else{
        [self tableView:self.lTableView scrollFollowTheOther:self.rTableView];
    }
}

- (void)tableView:(UITableView *)tableView scrollFollowTheOther:(UITableView *)other{
    CGFloat offsetY= other.contentOffset.y;
    CGPoint offset=tableView.contentOffset;
    offset.y=offsetY;
    tableView.contentOffset=offset;
}

- (UITableView *)lTableView {
    if(!_lTableView){
        _lTableView = [[UITableView alloc] init];
        _lTableView.frame = CGRectMake(LMargin, 0, ItemW*self.lTitles.count, CGRectGetHeight(self.frame));
        _lTableView.delegate = self;
        _lTableView.dataSource = self;
        _lTableView.showsVerticalScrollIndicator = NO;
        _lTableView.showsHorizontalScrollIndicator = NO;
        _lTableView.rowHeight = ItemH;
        _lTableView.backgroundColor = [UIColor blackColor];
        _lTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _lTableView.tableFooterView = [UIView new];
        [self addSubview:_lTableView];
    }
    return _lTableView;
}

- (UITableView *)rTableView {
    if(!_rTableView){
        _rTableView = [[UITableView alloc] init];
        _rTableView.frame = CGRectMake(0, 0, ItemW*self.rTitles.count, CGRectGetHeight(self.frame));
        _rTableView.delegate = self;
        _rTableView.dataSource = self;
        _rTableView.showsVerticalScrollIndicator = NO;
        _rTableView.showsHorizontalScrollIndicator = NO;
        _rTableView.rowHeight = ItemH;
        _rTableView.backgroundColor = [UIColor blackColor];
        _rTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _rTableView.tableFooterView = [UIView new];
        self.buttomScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(ItemW*self.lTitles.count, 0, SCREEN_W-ItemW*self.lTitles.count, CGRectGetHeight(self.frame))];
        
        self.buttomScrollView.contentSize = CGSizeMake(_rTableView.bounds.size.width, 0);
        self.buttomScrollView.backgroundColor = [UIColor blackColor];
        self.buttomScrollView.bounces = NO;
        self.buttomScrollView.showsHorizontalScrollIndicator = NO;
        
        [self.buttomScrollView addSubview:_rTableView];
        [self addSubview:self.buttomScrollView];
    }
    return _rTableView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
